var http = require('../../utils/http.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Addaddress: "",
        addrId: "",
        people: "",
        mobile: "",
        addr: "",
        editAddressList: {},
        visible: false,
        maskCloseAble: true,
        address: '',
        column: 3,
        region: ['广东省', '广州市', '海珠区'], // 初始省市区数据
        // customItem: '全部', // 自定义顶部显示项
    },
    bindRegionChange: function (e) {
        this.setData({
            region: e.detail.value,
            address: e.detail.value.toString().replaceAll(",", " "),
            Addaddress: e.detail
        })
        console.log(e);
    },
    // 滚动调接口
    handleGd(e) {
        console.log(e);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        this.setData({
            addrId: options.addrId
        })
        if (options.addrId) {
            http.request({
                url: `/p/address/addrInfo/${options.addrId}`,
                method: "get",
                data: {},
                callBack: res => {
                    console.log(res);
                    this.setData({
                        editAddressList: res,
                        people: res.receiver,
                        mobile: res.mobile,
                        addr: res.addr,
                        address: `${res.province} ${res.city} ${res.area}`
                    })
                }
            })
        }
    },
    handleBc() {
        // this.addressAarray=this.address.
        if (this.data.addrId) {
            http.request({
                url: `/p/address/updateAddr`,
                method: "put",
                data: {
                    addr: this.data.addr,
                    addrId: this.data.editAddressList.addrId,
                    area: this.data.address.split(" ")[2],
                    areaId: this.data.editAddressList.areaId,
                    city: this.data.address.split(" ")[1],
                    cityId: this.data.editAddressList.cityId,
                    mobile: this.data.mobile,
                    province: this.data.address.split(" ")[0],
                    provinceId: this.data.editAddressList.provinceId,
                    receiver: this.data.people,
                    userType: 0,
                },
                callBack: res => {
                    this.setData({
                        editAddressList: res,
                        address: `${res.province} ${res.city} ${res.area}`
                    })

                    setTimeout(() => {
                        wx.navigateBack({
                            // url:'/pages/delivery-address/delivery-address'
                            delta: 1
                        })
                    }, 100)
                }
            })
        } else {
            http.request({
                url: `/p/address/addAddr`,
                method: "post",
                data: {
                    addr: this.data.addr,
                    addrId: 0,
                    area: this.data.Addaddress.value[2],
                    areaId: Number(this.data.Addaddress.code[2]+'000000'),
                    city: this.data.Addaddress.value[1],
                    cityId: Number(this.data.Addaddress.code[1]+'000000'),
                    mobile: this.data.mobile,
                    province: this.data.Addaddress.value[0],
                    provinceId:  Number(this.data.Addaddress.code[0]+'000000'),
                    receiver: this.data.people,
                    userType: 0,
                },
                callBack: res => {
                    // this.setData({
                    //     editAddressList: res,
                    //     address: `${res.province} ${res.city} ${res.area}`
                    // })
                    setTimeout(() => {
                        wx.navigateBack({
                            // url:'/pages/delivery-address/delivery-address'
                            delta: 1
                        })
                    }, 100)
                }
            })
        }

    },
    handleDel() {
        wx.showModal({
            content: '确认删除此收获地址吗',
            success: (res) => {
                if (res.confirm) {
                    // console.log('户点击确定 ');
                    http.request({
                        url: `/p/address/deleteAddr/${this.data.editAddressList.addrId}`,
                        method: "DELETE",
                        data: {},
                        callBack: res => {
                            setTimeout(() => {
                                wx.navigateBack({
                                    // url:'/pages/delivery-address/delivery-address'
                                    delta: 1
                                })
                            }, 100)
                        }
                    })
                } else if (res.cancel) {
                    // console.log('用户点击取消 ');
                }
            }
        })

    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})